import { observable, action } from 'mobx';
import { SearchAdvanced, Comment,ListViewPage } from 'weaver-mobile-page';
import { PARAMS } from '../../components/public/BlogUtils';
import BlogCommentStore from '../comment/BlogCommentStore';

import * as Api from '../../api/BlogMainApi';

const initalState = {
    currentPage: 1,
    photoSwipePage: 1,
    isSearch: false,
    searchAdValue: '',
    searchAdValueShare:'',
    minUpdateid: null,
    mainPage:1,
    specialPage:1,
    shareToMePage:1,
    allBlogPage:1
}

export default class BlogMainStore {
    @observable organizationList = [];   // 关注列表展示信息
    @observable currentPage = initalState.currentPage;
    @observable organizationCurrentPage= 1;
    @observable photoSwipePage = initalState.photoSwipePage;//翻页 图片预览 微博已读
    @observable isSearch = initalState.isSearch;
    @observable searchAdValue = initalState.searchAdValue; // 搜索值
    @observable searchAdValueShare = initalState.searchAdValueShare; // 搜索值
    @observable blog = new BlogCommentStore({ getComment: this.getBlog });
    @observable unreadBlog = new BlogCommentStore({ getComment: this.getMyBlogHomepageNew });
    @observable shareToMe = new BlogCommentStore({ getComment: this.getShareToMeBlog });
    @observable specialBlog = new BlogCommentStore({ getComment: this.getSpecail });
    @observable allBlog = new BlogCommentStore({ getComment: this.getAllBlog });
    @observable search = new SearchAdvanced.Store({ route: '/blogMain/blog', id: 'adSearch' }); // 高级搜索
    @observable searchSpecial = new SearchAdvanced.Store({ route: '/blogMain/specialBlog', id: 'adSearchSpecial' }); // 高级搜索
    @observable searchShareToMe = new SearchAdvanced.Store({ route: '/blogMain/shareToMe', id: 'adSearchShare' }); // 高级搜索
    @observable organizationListStore = new ListViewPage.Store({ route:'/blogMain/organization', id:'myOrganization' });  // 我的关注列表
    @observable userPage = 1;  // 我的关注--搜索--接口请求页数
    @observable searchOrganization = new SearchAdvanced.Store({ route: '/blogMain/organization', id: 'adSearchOrganization' }); // 高级搜索
    @observable overviewAdValue = ''; // 微博总览-高级搜索值受控 -- 同时用于我的关注搜索值
    @observable mainPage = initalState.mainPage;
    @observable shareToMePage = initalState.shareToMePage;
    @observable allBlogPage = initalState.allBlogPage;
    @observable specialPage = initalState.specialPage;
    @observable minUpdateid = initalState.minUpdateid;
    @observable myOrganizationSearch = false; // 标识我的关注页面是否走了搜索
    @observable myOrganizationLoading = false;
    setIsNoParams = "2";
    @observable isTabs = false;
    @action setStoreState = (params) => {
        Object.keys(params).map(key => {
            this[key] = params[key];
        })
    }

    @action 
    setIsNoParamsAction = (params) => {
        this.setIsNoParams = params;
    }

    blogSearchCondition = (listType,params) => {
         let obj = {
            url:`/api/blog/base/blogSearchCondition`,
            method: `GET`,
            params: {...PARAMS, listType: listType,...params}
        };
        this.search.getData(obj)
    }


    @action('我的关注--搜索-列表 && 搜索-上拉刷新')
    myOrganizationSearch = () => {
        if (this.overviewAdValue === '' || !this.overviewAdValue) {
            this.myOrganizationSearch = false;
        }else{
            this.myOrganizationSearch = true;
            this.userPage = 1;
            this.getMyAttentionDatas();
        }
       
    }

    @action('组织结构-搜索所有人')
  getOrganizationDatas = (params) => {
    this.myOrganizationList.setState({loading: true});
    params = {
      searchUserName: this.overviewAdValue,
      organizationCurrentPage: this.userPage++,
      pageSize: 30
    }

    Api.getMyOrganizationUsers(params).then(res => {
      const {total, pageSize, organizationCurrentPage, organizationList, status} = res;
      if (status === '1') {
        const newState = {
          current: organizationCurrentPage,
          pageSize: pageSize,
          hasMore: total > pageSize * organizationCurrentPage,
          loading: false,
          template: {default: []},
          data: [...(organizationCurrentPage === 1 ? [] : this.organizationListStore.data), ...organizationList]
        }
        this.organizationListStore.setState(newState);
        this.organizationListStore.updateDataSource();
        this.myOrganizationLoading = false;
      }
    })
  }



    @action('微博主页列表请求')
    getBlog = () => {
        let searchParams;
        if(this.setIsNoParams=="1"){
            searchParams = {
                blogUserId: "",
                blogUserIdspan: "",
                content: "",
                deptId: "",
                deptIdspan: "",
                endDate: "",
                groupId: "all",
                startDate: "",
                subId: "",
                subIdspan: ""      
            }
            this.setIsNoParams = "2";
        }else{
            searchParams = this.search.form.formParams || {};
        }
        this.shareToMePage=1;this.allBlogPage=1;this.specialPage=1;
        let mainPage;
        if(this.isTabs){
            mainPage = 1;
        }else{
            mainPage = this.mainPage++;
        }
        if ( this.search.form.formParams.content !== ''||this.search.form.formParams) {
            let params = {
                pageSize: 10,
                currentPage: mainPage,
                ...searchParams,
            }
            if(typeof(params.groupId)=="undefined"||params.groupId=="all"||params.groupId=="shareToMe"||params.groupId=="unread"||params.groupId=='-5'){
                params.groupId="all";
            };
            return Api.getBlogHomepageListBySearch(params).then(data => {
                const { status, total, currentPage, pageSize } = data;
                if(status === '1' && total < currentPage * pageSize) {
                    let data_params = {hasMore: false}
                    Object.assign(data, data_params)
                }
                this.isTabs=false;
                this.photoSwipePage++;
                return data
            })
        } else {
            let currentPage;
            if(this.isTabs){
                currentPage = 1;
            }else{
                currentPage = this.currentPage++;
            }
            let params = {
                groupId:'all',
                currentPage: currentPage,
                pageSize: 10,
            }
            return Api.getBlogHomepageList(params).then(data => {
                const { status, total, currentPage, pageSize } = data;
                if(status === '1' && total < currentPage * pageSize) {
                    let data_params = {hasMore: false}
                    Object.assign(data, data_params)
                }
                this.isTabs=false;
                this.photoSwipePage++;
                return data
            })
        }
    }

    @action('分享给我列表请求')
    getShareToMeBlog = () => {  
        this.mainPage=1;this.allBlogPage=1;this.specialPage=1;
        let shareToMePage;
        if(this.isTabs){
            shareToMePage = 1;
            this.shareToMePage++;
        }else{
            shareToMePage = this.shareToMePage++;
        }
        let searchParams = this.search.form.formParams || {};
        let params = {
            pageSize: 10,
            currentPage: shareToMePage,
            ...searchParams,
        }
        params.groupId="shareToMe";
        return Api.getShareToMeList(params).then(data => {
            const { status, total, currentPage, pageSize } = data;
            if(status === '1' && total < currentPage * pageSize) {
                let data_params = {hasMore: false}
                Object.assign(data, data_params)
            }
            this.isTabs=false;
            this.photoSwipePage++;
            return data
        })
    }

    @action("特别关注列表请求")
    getSpecail = () => {
        let searchParams;
        let specialPage;
        if(this.isTabs){
            specialPage = 1;
        }else{
            this.specialPage++;
            specialPage = this.specialPage;
        }
        searchParams = this.search.form.formParams || {};
        if ( this.search.form.formParams.content !== ''||this.search.form.formParams) {
            let params = {
                pageSize: 10,
                currentPage: specialPage,
                ...searchParams,
            }
            params.groupId="-5";
            return Api.getBlogHomepageListBySearch(params).then(data => {
                const { status, total, currentPage, pageSize } = data;
                if(status === '1' && total < currentPage * pageSize) {
                    let data_params = {hasMore: false}
                    Object.assign(data, data_params)
                }
                this.isTabs=false;
                this.photoSwipePage++;
                return data
            })
        }
    }

    @action('分享给我列表请求')
    getAllBlog = () => {
        this.specialPage=1;
        this.shareToMePage=1;this.mainPage=1;
        let allBlogPage;
        if(this.isTabs){
            allBlogPage = 1;
        }else{
            allBlogPage = this.allBlogPage++;
        }
        let searchParams = this.search.form.formParams || {};
        let params = {
            pageSize: 10,
            currentPage: allBlogPage,
            ...searchParams,
        }
        params.groupId="0";
        return Api.getAllBlogList(params).then(data => {
            const { status, total, currentPage, pageSize } = data;
            if(status === '1' && total < currentPage * pageSize) {
                let data_params = {hasMore: false}
                Object.assign(data, data_params)
            }
            this.isTabs=false;
            this.photoSwipePage++;
            return data
        })
    }


    @action('微博主页未读列表请求')
    getMyBlogHomepageNew = () => {
        let currentPage;
        if(this.isTabs){
            currentPage = 1;
        }else{
            currentPage = this.currentPage++;
        }
        let params = {
            currentPage: currentPage,
            pageSize: 10,
        }
        this.minUpdateid && (params = {...params, minUpdateid: this.minUpdateid});
        return Api.getMyBlogHomepageNew(params).then(data => {
            if(data.status === '1' && data.replyList.length < params.pageSize) {
                let data_params = {hasMore: false}
                Object.assign(data, data_params)
            }
            this.minUpdateid = data.minUpdateid;
            this.photoSwipePage++;
            this.isTabs=false;
            return data;
        })
    }

    setBlogReaded = (params,callback) => {
        Api.setBlogReaded(params).then(data => {
            if(callback) callback();
        })
    }

    @action('关联高级搜索与前端搜索条件')
    updateAdSearchSubject = (value) => {
        this.searchAdValue = value;
        this.search.form.updateField('content', value);
    }

    @action('关联高级搜索与前端搜索条件')
    updateAdSearchShareSubject = (value) => {
        this.searchAdValueShare = value;
        this.searchShareToMe.form.updateField('content', value);
    }

    @action('搜索')
    searchBlog = () => {
        this.isSearch = true;
        this.mainPage = 1;
        this.blog.blogCommentStore.getDatas();
    }

    @action('搜索')
    searchSpecailBlog = () => {
        this.isSearch = true;
        this.specialPage = 1;
        this.specialBlog.blogCommentStore.getDatas();
    }

    @action('搜索分享给我')
    searchShareToMeBlog = () => {
        this.isSearch = true;
        this.shareToMePage = 1;
        this.shareToMe.blogCommentStore.getDatas();
    }

    @action('搜索未读')
    searchUnReadBlog = () => {
        this.isSearch = true;
        this.unreadBlog.blogCommentStore.getDatas();
    }


    @action('搜索全部微博')
    searchAllBlogBlog = () => {
        this.isSearch = true;
        this.allBlogPage = 1;
        this.allBlog.blogCommentStore.getDatas();
    }


    // 初始化数据
    initCondition = () => {
        this.setStoreState(initalState);
    }
}